package test;

public class FalseSharingExample2 implements Runnable {
    private static final int NUM_THREADS = 2;
    private static final int NUM_ITERATIONS = 100000000;
    private static final int ARRAY_SIZE = 16;

    private static final long[] longArray = new long[ARRAY_SIZE];
    private final int startIndex;

    public FalseSharingExample2(int startIndex) {
        this.startIndex = startIndex;
    }

    @Override
    public void run() {
        for (int i = 0; i < NUM_ITERATIONS; i++) {
            longArray[startIndex * 2] = i;
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread[] threads = new Thread[NUM_THREADS];
        for (int i = 0; i < NUM_THREADS; i++) {
            threads[i] = new Thread(new FalseSharingExample2(i));
        }
        long startTime = System.nanoTime();
        for (Thread thread : threads) {
            thread.start();
        }
        for (Thread thread : threads) {
            thread.join();
        }

        long endTime = System.nanoTime();
        System.out.println("Time taken: " + (endTime - startTime) + " ns");//Time taken: 9865146 ns
    }
}
